package com.liuh.dao.impl;

import java.util.List;
import java.util.Map;
import java.util.Set;

import com.liuh.dao.BaseDao;
import com.liuh.dao.CategoryInfoDao;
import com.liuh.pojo.CategoryInfo;
import com.liuh.pojo.OrderInfo;
import com.liuh.pojo.ProductInfo;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Dict;
import cn.hutool.core.util.StrUtil;

public class CategoryInfoDaoImpl extends BaseDao implements CategoryInfoDao{

	@Override
	public List<CategoryInfo> cateInfoByParentId(int id) {
		// TODO Auto-generated method stub
		String sql="select * from category_info where parentId=?";
		return executeQuery(sql, CategoryInfo.class, id);
	}

	@Override
	public List<OrderInfo> orders() {
		// TODO Auto-generated method stub
		StringBuffer sbBuffer=new StringBuffer();
		sbBuffer.append("select a1.*,a2.quantity 'productNum',a2.cost 'productTotalCost',a3.`name`,a3.fileName from easybuy_order a1 ");
		sbBuffer.append("LEFT JOIN easybuy_order_detail a2 on a1.id=a2.orderId ");
		sbBuffer.append("LEFT JOIN easybuy_product a3 on a2.productId=a3.id ");
		return executeQuery(sbBuffer.toString(), OrderInfo.class);
	}

	@Override
	public List<CategoryInfo> cateInfoByProId(int id) {
		// TODO Auto-generated method stub
		StringBuffer sbBuffer=new StringBuffer();
		sbBuffer.append("select * from category_info where parentId in( ");
		sbBuffer.append("select categoryLevel1Id from easybuy_product where id=? ");
		sbBuffer.append("UNION ");
		sbBuffer.append("select categoryLevel2Id from easybuy_product where id=? ");
		sbBuffer.append("UNION ");
		sbBuffer.append("select 0 ");
		sbBuffer.append(")ORDER BY type ");
		return executeQuery(sbBuffer.toString(), CategoryInfo.class, id, id);
	}

	@Override
	public Dict cateInfoByProId2(int id) {
		// TODO Auto-generated method stub
		String sql = "select * from easybuy_product where id=? ";
		List<Object> params = CollUtil.newArrayList();
		params.add(id);
		List<Map<String, Object>> list = executeQuery(sql, params.toArray());
		if(list.size()>0) {
			return Dict.create().set("cate1", list.get(0).get("categoryLevel1Id"))
					.set("cate2", list.get(0).get("categoryLevel2Id")).set("cate3", list.get(0).get("categoryLevel3Id"));
		}else {
			return Dict.create();
		}
	}

	@Override
	public List<CategoryInfo> cateInfoPage(int pageIndex, int pageSize) {
		// TODO Auto-generated method stub
		String sql="select * from category_info limit ?,?";
		return executeQuery(sql,CategoryInfo.class, pageIndex,pageSize);
	}

	@Override
	public int cateInfoCount() {
		// TODO Auto-generated method stub
		String sql="select count(1) 'c1' from category_info";
		List<Map<String, Object>> list =executeQuery(sql, CollUtil.newArrayList().toArray());
		return Convert.toInt(list.get(0).get("c1"));
	}

	@Override
	public List<CategoryInfo> cateInfoBy12() {
		// TODO Auto-generated method stub
		String sql="select * from category_info where type in(1,2)ORDER BY type ";
		return executeQuery(sql, CategoryInfo.class, CollUtil.newArrayList().toArray());
	}

	@Override
	public List<CategoryInfo> findCateInfoAndParentId(int id) {
		// TODO Auto-generated method stub
		StringBuffer sbBuffer=new StringBuffer();
		sbBuffer.append("select a1.*,a2.id 'cate1Id',a2.name 'cate1Name',a3.id 'cate2Id',a3.`name` 'cate2Name' from category_info a1 ");
		sbBuffer.append("LEFT JOIN category_info a2 on a1.parentId=a2.id ");
		sbBuffer.append("LEFT JOIN category_info a3 on a2.parentId=a3.id ");
		sbBuffer.append("where a1.id=? ");
		return executeQuery(sbBuffer.toString(), CategoryInfo.class, id);
	}

	@Override
	public List<ProductInfo> findProductByIds(Set<String> ids) {
		// TODO Auto-generated method stub
		String sql="select * from easybuy_product where id in (" + StrUtil.repeatAndJoin("?", ids.size(), ",") + ")";
		return executeQuery(sql, ProductInfo.class, ids.toArray());
	}

}
